﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace TrabiBT
{
    public partial class ViewVariableChart : UserControl,IView
    {
        double time = 0;
        Variable myVar;
        double axisYMin = 0;
        double axisYMax = 1000;
        double axisXMax = 20;
        public ViewVariableChart()
        {
            InitializeComponent();
        }

        public ViewVariableChart(Variable var)
        {
            InitializeComponent();
            this.myVar = var;
            dataChart.Series[0].ChartType = SeriesChartType.FastLine;
            dataChart.ChartAreas[0].AxisX.Maximum = 5;
            dataChart.ChartAreas[0].AxisX.Minimum = 0;
            dataChart.Visible = true;
        }

        public void UpdateD()
        {
            time += 0.2;
            if (myVar.CurrentValue != null)
            {
                if(myVar.Type == "float")
                {
                    this.dataChart.Series[0].Points.Add(new DataPoint(time, (float)myVar.CurrentValue));
                }
                else if (myVar.Type == "int32")
                {
                    this.dataChart.Series[0].Points.Add(new DataPoint(time, (int)myVar.CurrentValue));
                }
            }
            if (time > axisXMax && dataChart.Series.ElementAt(0).Points.Count != 0)
                {
                    dataChart.Series.ElementAt(0).Points.RemoveAt(0);
                    dataChart.ChartAreas[0].AxisX.Minimum = (int)time - (axisXMax - 1);
                    dataChart.ChartAreas[0].AxisX.Maximum = (int)time + 1;
                    dataChart.ChartAreas[0].AxisY.Minimum = axisYMin;
                    dataChart.ChartAreas[0].AxisY.Maximum = axisYMax;
                }
            
        }


        private void refreshBtn_Click(object sender, EventArgs e)
        {
            if (Convert.ToDouble(tbMaxYAxis.Text) > axisYMin && Convert.ToDouble(tbMaxYAxis.Text) != 0)
            {
                axisYMax = Convert.ToDouble(tbMaxYAxis.Text);
            }
            if (Convert.ToDouble(tbMinYAxis.Text) < axisYMax)
            {
                axisYMin = Convert.ToDouble(tbMinYAxis.Text);
            }
            if (Convert.ToDouble(tbTimer.Text) > 0 && tbTimer.Text != null)
            {
                axisXMax = Convert.ToDouble(tbTimer.Text);
            }
        }
    }
}
